<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="app-nav-bar"
      title="注册 / 登录"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form 
    ref="loginForm"
    @submit="onLogin"
    :show-error="false"
    :show-error-message="false"
    :validate-first='true'
    @failed="onFailed">
      <van-field
        v-model="user.mobile"
        icon-prefix="toutiao"
        left-icon="shouji"
        name="mobile"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      />
      <van-field
        v-model="user.code"
        clearable
        icon-prefix="toutiao"
        left-icon="yanzhengma"
        placeholder="请输入验证码"
        name="code"
        :rules="rules.code"
      >
        <template #button>
          <van-count-down
          :time="1000*60"
          format=" ss s"
          v-if="isShowCountDown"
          @finish="isShowCountDown = false"
          />
          <van-button
            v-else
            class="send-btn"
            size="mini"
            round
            :loading = "isSendLoading"
            @click.prevent="sendCode"
          >发送验证码</van-button>
        </template>
      </van-field>
       <div class="login-btn-wrap">
        <van-button class="login-btn" type="info" block>登录</van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
import { login,sendSms } from '@/api/user'

export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '', // 手机号
        code: '' ,// 验证码
      },
      isShowCountDown:false,
      isSendLoading:false,
      rules:{
        mobile:[
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3|5|7|8|9|]\d{9}$/,message:'手机格式错误'}
          ],
        code:[
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/,message:'验证码格式错误'}
          ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onLogin () {
      this.$toast.loading({
      message: '登录中...',
      duration:0,
      forbidClick: true,
      });
      try {
        const {data} = await login(this.user)
        this.$toast.success('登录成功')
        this.$store.commit('setUser',data.data)
      } catch (error) {
        this.$toast.fail('登录失败，手机号或验证码错误')
      }
    },
    onFailed(err){
      console.log(err)
      this.$toast({
        message:err.errors[0].message,
        position:'top'
      })
    },
    // 发送验证码
    async sendCode(){
      try {
        await this.$refs['loginForm'].validate('mobile')
        this.isSendLoading = true
        await sendSms(this.user.mobile)
        this.isShowCountDown = true
      } catch (err) {
        let message = ''
        console.log(err)
        if(err && err.response && err.response.status===429){
          message = '您发送太频繁了'
        }
        else if(err.name == 'mobile'){
          message = err.message
        }
        else{
          message = '发送失败'
        }
        this.$toast({
          message,
          position:'top'
        })
      }
      this.isSendLoading = false
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .send-btn {
    width: 76px;
    height: 23px;
    background-color: #ededed;
    .van-button__text {
      font-size: 11px;
      color: #666666;
    }
  }
  .login-btn-wrap {
    padding: 26px 16px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
      .van-button__text {
        font-size: 15px;
      }
    }
  }
}
</style>